<?php

/**
 * 社群管理
 * Class CommunityAction
 * by warcello
 * create 2016-10-12
 */
class CommunityAction extends Action {

	/**
	 * 获取社群列表
	 * @param string $user_name 查询群主名称
	 * @param string $group_name  查询群名称
	 * @param string $order
	 * @param string $orderBy
	 * @param int $start
	 * @param int $limit
	 */
	public function getcomMunity($user_name="",$group_name="",$order="updateTime", $orderBy="desc", $start = 0, $limit = 25)
	{
		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}

		$cid = Session::$user["customerId"];
		Session::switchCustomer(0);
		$CommunityModel = M("community");
		$CommunityUser = M("community_user");
		Session::switchCustomer($cid);

		$condition["customerId"] = array("eq",$cid);
		$condition["deleted"] = array("eq",0);
		if($user_name != "") {
			$condition["winfo.nickname"] = array('like','%'.trim($user_name).'%');
		}
		if($group_name != "") {
			$condition["community.groupName"] = array('like','%'.trim($group_name).'%');
		}
		//获取社群基本信息
		$arr = $CommunityModel->alias('community')
				->join('LEFT JOIN wx_'.$cid.'_user_weixin_info winfo on winfo.openid = community.userName')
				->where($condition)
				->field('community.*,winfo.nickname,winfo.headimgurl')
				->order("$order $orderBy")
				->limit("$start,$limit")->select();

		if ($arr) {
			$count = $CommunityModel->where($condition)->count();
			Output::instance()->objArray($arr, $count);
		} else {
			Output::instance()->resultCode(code_success);
		}
	}

	/**
	 * 查询用户列表
	 * @param $gid
	 * @param string $order
	 * @param string $orderBy
	 * @param int $start
	 * @param int $limit
	 */
	public function getGroupListUser($gid,$order="clickTime", $orderBy="desc", $start = 0, $limit = 10){

		$retVal = Session::init();
		if (!$retVal) {
			Output::instance()->resultCode(code_access_denied);
			return;
		}

		$cid = Session::$user["customerId"];
		Session::switchCustomer(0);
		$CommunityModel = M("community");
		$CommunityUser = M("community_user");
		Session::switchCustomer($cid);

		$groupinfo = $CommunityModel->where(['id'=>$gid])->find();
		$condition['c.groupId'] = array('eq',$gid);
		$result = $CommunityUser->alias("c")
				->join('LEFT JOIN wx_'.$cid.'_user_weixin_info winfo on winfo.openid = c.userName')
				->where($condition)
				->field('c.*,winfo.nickname,winfo.headimgurl')
				->order("$order $orderBy")
				->limit("$start,$limit")->select();
		foreach($result as $key => $value){
			//若签到时间小于分享时间则视为不活跃用户
			if($value['clickTime']<$groupinfo['shareTime']){
				$result[$key]['clickTime']='';
			}
		}

		$count = $CommunityUser->where(['groupId'=>$gid])->count('id');
		Output::instance()->objArray($result, $count);
	}

	/**
	 * 群组删除功能
	 * @param $gid
	 */
	public function communeDeleteGroup($gid){

		$cid = Session::$user['customerId'];
		Session::switchCustomer(0);
		$CommuneModel = M('community');
		Session::switchCustomer($cid);

		$deletGroup = $CommuneModel->where(['id'=>$gid])->save(['deleted'=>1]);
		if($deletGroup){
			Output::instance()->resultCode();
		}else{
			Output::instance()->resultCode(code_sql_error);
		}
	}
}
